/** @file letter.cpp
*/
#include "letter.h"

Letter::Letter(HWND parent, char *text):Widget(parent)
{
    activable =1;
    
    font = font_STANDARD;
    name ="Letter";
    character =text;

    keyShift =0;
}

Letter::~Letter()
{
}

void Letter::setFont(FONT *font)
{
    this->font =font;
    rect.x1 =rect.x0 + font->width +1;
    rect.y1 =rect.y0 + font->height;
}

bool Letter::isDigit() const
{
    return digitMode;
}
void Letter::setMode(bool digit)
{
    digitMode =digit;
}

short Letter::sizeX()
{
    return font->width +1;
}

void Letter::setChar(char *c)
{
    character =c;
}

void Letter::draw(Rect *r)
{
    if( (*character <' ') || (*character > 'z'))
        return;
    
    if(isActive())
    {
         pDC->drawFont(rect.x0,rect.y0, character, 0x00,0x0F, font);
    }else
    {
        pDC->drawFont(rect.x0,rect.y0, character, 0x0F,0x00, font);
    }
    
    pDC->dirty(max(r->x0,rect.x0), max(r->y0,rect.y0), min(r->x1,rect.x1), min(r->y1,rect.y1));
    Widget::draw(r);
}

int Letter::eventHandler(HWND hWnd, int iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
        //key press event
    case MSG_KEY_DOWN:
        switch(wParam)
        {
        case KEY_UP:
            if( (*character >' ') && !digitMode)
            {
                (*character) --;
                signal[SIGNAL_LETTER_CHANGED].emitSignal();
            }
            else if( (*character >'0') && digitMode)
            {
                (*character) --;
                signal[SIGNAL_LETTER_CHANGED].emitSignal();
            }
            draw(&rect);
            break;
        case KEY_DOWN:
            if( (*character <'z') && !digitMode)
            {
                (*character) ++;
                signal[SIGNAL_LETTER_CHANGED].emitSignal();
            }
            else if( (*character <'9') && digitMode)
            {
                (*character) ++;
                signal[SIGNAL_LETTER_CHANGED].emitSignal();
            }
            draw(&rect);
            break;

        default:
            if(keyShift)
            {
                switch(wParam)
                {
                    ///< 3key to 6 key emulation
                case KEY_ESC:
                    keyShift =0;
                    break;

                case KEY_ENTER:
                    sendMessage(this, MSG_KEY_DOWN, KEY_ESC,0);
                    break;
                case KEY_PREV:
                    sendMessage(this, MSG_KEY_DOWN, KEY_UP, 0);
                    break;
                case KEY_NEXT:
                    sendMessage(this, MSG_KEY_DOWN, KEY_DOWN, 0);
                    break;
                }
            }
            else
            {
#ifdef CONFIG_KEY_SHIFT
                switch(wParam)
                {
                 case KEY_ENTER:
                    keyShift =1;
                     break;
                }
#endif
                return Widget::eventHandler(hWnd, iMsg, wParam, lParam);
            }
            break;
        }
    }
    
    return 0;
}

void Letter::setupWidgets()
{
}

